<script setup>
import { getEnum } from '@/utils/enumUtils'
import { onMounted, ref} from "vue";
import {apiSelectOriginList} from "./api";

let optionList = ref([])

const props = defineProps({
  dictionary: String,
  originConfig: {
    type: Object,
    default: {
      baseUrl: null,
      method: null,
      url: null,
    }
  },
  formatter: {
    type: Function
  },
})


const initOptions = async () => {
  const { originConfig, dictionary, formatter } = props
  const {url, baseUrl, method} = originConfig
  if (url) {
    apiSelectOriginList({
      method: method || 'post',
      url: url,
      baseURL: baseUrl
    }).then(res => {
      if (formatter) {
        optionList.value = formatter(res.data) || []
      } else {
        optionList.value = res.data
      }
    })
  }
  if (dictionary) {
    getEnum(dictionary, res => {
      optionList.value = formatter ? formatter(res[dictionary] ) : res[dictionary]
    })
  }
}

onMounted(() => {
  initOptions()
})
</script>

<template>
  <el-checkbox-group v-bind="$attrs">
    <el-checkbox v-for="item in optionList" :label="item.value">
      {{item.label}}
    </el-checkbox>
  </el-checkbox-group>
</template>

<style scoped lang="scss">

</style>